package com.android.aqq.util;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import android.app.Service;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.android.aqq.R;
import com.android.aqq.bean.VerifyImage;

import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.qq.beans.IMMessage;

public class Utils {
	public static String getStatus(byte status) {
		switch(status) {
		case QQ.QQ_STATUS_ONLINE:
			return "在线";
		case QQ.QQ_STATUS_OFFLINE:
			return "离线";
		case QQ.QQ_STATUS_AWAY:
			return "离开";
		case QQ.QQ_STATUS_BUSY:
			return "忙碌";
		case QQ.QQ_STATUS_QME:
			return "Q我吧";
		case QQ.QQ_STATUS_QUIET:
			return "请勿打扰";
		case QQ.QQ_STATUS_HIDDEN:
			return "隐身";
		}
		return "";
	}
	
	public static int getStatusOrder(byte status) {
		switch(status) {
		case QQ.QQ_STATUS_QME:
			return 1;
		case QQ.QQ_STATUS_ONLINE:
			return 2;
		case QQ.QQ_STATUS_OFFLINE:
			return 7;
		case QQ.QQ_STATUS_AWAY:
			return 5;
		case QQ.QQ_STATUS_BUSY:
			return 3;
		case QQ.QQ_STATUS_QUIET:
			return 4;
		case QQ.QQ_STATUS_HIDDEN:
			return 6;
		default:
			return 9;
		}
	}
	
	public static VerifyImage downloadVerifyImage(String fileUrl) {
		try {
			URL myFileUrl = new URL(fileUrl);
			HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
			conn.setDoInput(true);
			conn.connect();
			int length = conn.getContentLength();
			InputStream is = conn.getInputStream();
			Bitmap bmImg = BitmapFactory.decodeStream(is);
			String sessionId= conn.getHeaderField("getqqsession");
			VerifyImage image= new VerifyImage(sessionId, bmImg);
			return image;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static boolean isNetworkAvailable(Service service) {
		Context context = service.getApplicationContext();
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			return false;
		} else {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public static String parseIMMessage(Context context,List<IMMessage> messages) {
		StringBuffer sb = new StringBuffer();
		int size = messages.size();
		for (int i=0; i<size; i++) { 
			IMMessage message = messages.get(i); 
			switch(message.getType()) {
			case 02:
				sb.append(getResourceString(context , "face" + message.toString()));
				break;
			default:
				sb.append(message);
				break;
			}
		}
		return sb.toString();
	}
	
	public static String getResourceString(Context context, String key) {
		try {
			return context.getResources().getString(getResourceStringId(key));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public static int getResourceStringId(String key) {
		try {
			return  Integer.parseInt(R.string.class.getField(key).get(null).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
}
